/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean.session;

import bean.managed.SystemMessageController;
import entity.DepartmentEntity;
import entity.InternalMessageEntity;
import entity.InternalMessageReceivedEntity;
import entity.SystemUserEntity;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import type.MessageType;

/**
 *
 * @author wei
 */
@Stateless
public class InternalMessageEntityFacade extends AbstractFacade<InternalMessageEntity> {

    @PersistenceContext(unitName = "MacTeam-warPU")
    private EntityManager em;
    @EJB
    SystemUserEntityFacade userFacade;

    protected EntityManager getEntityManager() {
        return em;
    }

    public InternalMessageEntityFacade() {
        super(InternalMessageEntity.class);
    }

    public void createAndSendSystemMessage(SystemUserEntity to,
            String message, String pageToRedirectTo, SystemMessageController sysMessageController) {
        InternalMessageEntity msg = new InternalMessageEntity();
        ArrayList<SystemUserEntity> recipients = new ArrayList<SystemUserEntity>();
        recipients.add(to);
        msg.setRecipients(recipients);
        msg.setTitle(message);
        msg.setBody(pageToRedirectTo);
        msg.setMessageType(MessageType.SystemMessage);

        SystemUserEntity admin = userFacade.getSystemUserByName("admin");
        msg.setSender(admin);
        create(msg);
        msg.setSentAt(new Date());
        edit(msg);

        //update user and create msg instances
        to.getMessagesReceived().add(msg);
        InternalMessageReceivedEntity msgInstance = new InternalMessageReceivedEntity();
        msgInstance.setReceiver(to);
        msgInstance.setMsg(msg);
        createInternalMessageReceivedEntity(msgInstance);
        to.getMessagesReceivedInstances().add(msgInstance);

        userFacade.edit(to);
        if(sysMessageController!=null)
            sysMessageController.populateInbox(); //hack for demo sake
    }

    public void createInternalMessageReceivedEntity(InternalMessageReceivedEntity instance) {
        getEntityManager().persist(instance);
    }

    public void editInternalMessageReceivedEntity(InternalMessageReceivedEntity instance) {
        getEntityManager().merge(instance);
    }

    public List<InternalMessageEntity> getDraftForUser(SystemUserEntity u) {
        String queryStr = "Select m from InternalMessage m, SystemUser u "
                + "where m.sender=u and m.sentAt is null and m.archived=false"
                + " and u.id=" + u.getId();
        Query query = em.createQuery(queryStr);
        return query.getResultList();
    }

    public List<InternalMessageEntity> getAnnouncementInboxForUser(SystemUserEntity u) {
        DepartmentEntity d = u.getDepartment();
        System.out.println(d);

        String queryStr = "SELECT p FROM InternalMessage p INNER JOIN p.departmentRecipients e "
                + " WHERE e.id=" + d.getId()
                + " and p.sentAt is not null "
                + " and p.archived=false"
                + " and p.messageType=:type";
        Query query = em.createQuery(queryStr);
        query.setParameter("type", MessageType.Announcement);
        return query.getResultList();
    }
    
    public List<InternalMessageEntity> getSystemMessageInboxForUser(SystemUserEntity u) {
        return getInboxForUser(u, MessageType.SystemMessage);
    }
    
    public List<InternalMessageEntity> getUserMessageInboxForUser(SystemUserEntity u) {
        return getInboxForUser(u, MessageType.UserMessage);
    }
    
    private List<InternalMessageEntity> getInboxForUser(SystemUserEntity u, MessageType type) {
        String queryStr = "SELECT p FROM InternalMessage p INNER JOIN p.recipients e "
                + " WHERE e.id=" + u.getId()
                + " and p.sentAt is not null "
                + " and p.archived=false"
                + " and p.messageType=:type";
        Query query = em.createQuery(queryStr);
        query.setParameter("type", type);
        return query.getResultList();
    }
}
